package com.downhometech.productcalculator.activities;

import java.text.DecimalFormat;

import com.downhometech.productcalculator.controllers.ProductController;
import com.downhometech.productcalculator.models.Cart;
import com.downhometech.productcalculator.models.Setting;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class EditSettingsActivity extends DefaultActivity
{
	private EditText taxRate = null;
	private EditText shippingRate = null;
	private CheckBox taxRateIsPercent = null;
	private CheckBox shippingRateIsPercent = null;
	private CheckBox showProductNumbers = null;
	private Cart cart = null;
	
	public void onCreate(Bundle savedInstanceState) 
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.edit_settings);
	    
	    ProductCalculator application = ((ProductCalculator)getApplicationContext());
	    cart = application.getProductCart();
	    taxRate = (EditText) findViewById(R.id.TaxRateOption);
	    shippingRate = (EditText) findViewById(R.id.ShippingRateOption);
	    taxRateIsPercent = (CheckBox)findViewById(R.id.TaxRateIsPercent);
	    shippingRateIsPercent = (CheckBox)findViewById(R.id.ShippingRateIsPercent);
	    
	    double currTaxRate = cart.getTaxRate();
	    double currShippingRate = cart.getShippingRate();
	    
	    if (cart.isTaxRateIsPercent()) { currTaxRate = cart.getTaxRate() * 100; }
	    if (cart.isShippingRateIsPercent()) { currShippingRate = cart.getShippingRate() * 100; }
	    
	    taxRate.setText(ProductController.formatDouble(currTaxRate, "##.0000") + "");
	    shippingRate.setText(ProductController.formatDouble(currShippingRate, "##.0000") + "");
	    taxRateIsPercent.setChecked(cart.isTaxRateIsPercent());
	    shippingRateIsPercent.setChecked(cart.isShippingRateIsPercent());
		
	    Button saveButton = (Button) findViewById(R.id.EditSettingsSave);
	    saveButton.setOnClickListener(new View.OnClickListener() {
	 	   public void onClick(View view) {
	 		   
	 		   double fieldTaxRate = ProductController.getDouble(taxRate);
	 		   double fieldShippingRate = ProductController.getDouble(shippingRate);
	 		   
	 		   if (taxRateIsPercent.isChecked() && fieldTaxRate > 1) { fieldTaxRate = fieldTaxRate / 100; }
	 		   if (shippingRateIsPercent.isChecked() && fieldShippingRate > 1) { fieldShippingRate = fieldShippingRate / 100; }
	 		   
	 		   cart.setTaxRate(fieldTaxRate);
	 		   cart.setShippingRate(fieldShippingRate);
	 		   cart.setShippingRateIsPercent(shippingRateIsPercent.isChecked());
	 		   cart.setTaxRateIsPercent(taxRateIsPercent.isChecked());
	 		   Intent intent = new Intent(EditSettingsActivity.this, CartActivity.class);
	 		   startActivity(intent);				
	 		   finish();
	 	   }
	    });
	    
	    Button savePermButton = (Button) findViewById(R.id.EditSettingsSavePermanent);
	    savePermButton.setOnClickListener(new View.OnClickListener() {
	 	   public void onClick(View view) {
	 		   
	 		   Setting setting;
	 		   double fieldTaxRate = ProductController.getDouble(taxRate);
	 		   double fieldShippingRate = ProductController.getDouble(shippingRate);
	 		   
	 		   if (taxRateIsPercent.isChecked() && fieldTaxRate > 1) { fieldTaxRate = fieldTaxRate / 100; }
	 		   if (shippingRateIsPercent.isChecked() && fieldShippingRate > 1) { fieldShippingRate = fieldShippingRate / 100; }
	 		   
	 		   cart.setTaxRate(fieldTaxRate);
	 		   cart.setShippingRate(fieldShippingRate);
	 		   cart.setShippingRateIsPercent(shippingRateIsPercent.isChecked());
	 		   cart.setTaxRateIsPercent(taxRateIsPercent.isChecked());
	 		   
	 		   ProductController controller = new ProductController(EditSettingsActivity.this);
	 		   setting = controller.getSettingById(Setting.DEFAULT_TAX_RATE_ID);
	 		   setting.setValue(taxRate.getText().toString());
	 		   controller.saveSetting(setting, Setting.DEFAULT_TAX_RATE_ID);
	 		   
	 		   setting = controller.getSettingById(Setting.DEFAULT_TAX_RATE_IS_PERCENT_ID);
	 		   setting.setValue(taxRateIsPercent.isChecked() + "");
	 		   controller.saveSetting(setting, Setting.DEFAULT_TAX_RATE_IS_PERCENT_ID);
	 		   
	 		   setting = controller.getSettingById(Setting.DEFAULT_SHIPPING_RATE_ID);
	 		   setting.setValue(shippingRate.getText().toString());
	 		   controller.saveSetting(setting, Setting.DEFAULT_SHIPPING_RATE_ID);
	 		   
	 		   setting = controller.getSettingById(Setting.DEFAULT_SHIPPING_RATE_IS_PERCENT_ID);
	 		   setting.setValue(shippingRateIsPercent.isChecked() + "");
	 		   controller.saveSetting(setting, Setting.DEFAULT_SHIPPING_RATE_IS_PERCENT_ID);
	 		   
	 		   
	 		   Intent intent = new Intent(EditSettingsActivity.this, CartActivity.class);
	 		   startActivity(intent);				
	 		   finish();
	 	   }
	    });
	    
	    Button cancelButton = (Button) findViewById(R.id.ButtonCancel);
	    cancelButton.setOnClickListener(new View.OnClickListener() {
	 	   public void onClick(View view) {
	 		   Intent intent = new Intent(EditSettingsActivity.this, CartActivity.class);
	 		   startActivity(intent);				
	 		   finish();
	 	   }
	    });	 	    
	}
}

